package com.c.service.admin;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.pool.DruidDataSource;
import com.c.dto.PageDto;
import com.c.model.User;
import com.c.result.LayResult;

import cn.hutool.db.Entity;
import cn.hutool.db.Session;

@Service
public class UserService {

	@Autowired
	private DruidDataSource ds;

	public LayResult<User> getUserList(PageDto pg) {
		Session session = Session.create(ds);
		LayResult<User> lr = new LayResult<User>();
		List<User> rList = new ArrayList<User>();
		try {

			List<Entity> result = session.findAll("user");
			for (int i = 0; i < result.size(); i++) {
				User user = new User();
				Entity entity = result.get(i);
				user.setId(Integer.valueOf("" + entity.get("ID")));
				user.setUsername(entity.getStr("USERNAME"));
				user.setType(Integer.valueOf("" + entity.get("TYPE")));
				rList.add(user);
			}
			lr.setData(rList);
			lr.setCode(0);
			lr.setMsg("");
		} catch (SQLException e) {
			e.printStackTrace();
			lr.setMsg(e.getMessage());
		} finally {
			session.close();
		}
		return lr;
	}

}
